package com.zhejiangzhengyuan.municipal_flooded_road.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhejiangzhengyuan.municipal_common.entity.Device;
import com.zhejiangzhengyuan.municipal_common.entity.LedHistory;
import com.zhejiangzhengyuan.municipal_common.entity.vo.DeviceVo;
import com.zhejiangzhengyuan.municipal_common.response.ResponseVo;
import com.zhejiangzhengyuan.municipal_common.service.DeviceService;
import com.zhejiangzhengyuan.municipal_common.service.LedHistoryService;
import com.zhejiangzhengyuan.municipal_common.utils.ConstantUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/** @author lin_ju Create on 2019/12/30 */
@RestController
@RequestMapping("/floodedRoad/deviceManagement")
@Api(tags = "道路积水设备管理")
@Slf4j
public class FloodRoadDeviceManagementController {
  @Autowired private DeviceService deviceService;
  @Autowired private LedHistoryService ledHistoryService;
  private String deviceType = ConstantUtil.DEVICE_TYPE.FLOODED_ROAD;

  @ApiResponse(code = 200, message = "操作成功", response = Device.class)
  @ApiOperation("添加设备Api")
  @PostMapping("/saveDevice")
  public ResponseVo save(
      @ApiParam(name = "添加设备相关字段", required = true) @RequestBody @Validated Device device) {
    // 设备添加失败则返回服务器端错误
    device.setType(deviceType);
    device.setName(deviceType);
    return deviceService.saveDevice(device);
  }

  @ApiOperation("根据ID查找设备")
  @GetMapping("/findDeviceById/{id}")
  public ResponseVo findDeviceById(
      @ApiParam(value = "设备Id", required = true) @PathVariable Integer id) {
    Device device = deviceService.getById(id);
    return device == null
        ? ResponseVo.NOT_FOUND_RESOURCES
        : new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, device);
  }

  @ApiOperation("根据ID更新设备")
  @PostMapping("/updateDevice")
  public ResponseVo updateDevice(@RequestBody @Validated Device device) {

    return deviceService.updateDevice(device) > 0
        ? new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, device)
        : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("根据ID删除设备")
  @DeleteMapping("/removeDeviceById/{id}")
  public ResponseVo removeDeviceById(@PathVariable Integer id) {
    return deviceService.removeById(id) ? ResponseVo.SUCCESS : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("根据ID 批量删除设备 格式：'ids':[7,9,10]")
  @DeleteMapping("/removeDeviceByIds")
  public ResponseVo removeDeviceByIds(@RequestBody Map<String, List<Integer>> map) {
    List<Integer> idList = map.get("ids");
    return deviceService.removeByIds(idList) ? ResponseVo.SUCCESS : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("查询设备跟设施")
  @GetMapping("/selectAllDevice")
  public Page<DeviceVo> selectAllDevice(Integer page, Integer limit) {
    page = page == null || page <= 0 ? 1 : page;
    limit = limit == null || limit <= 0 ? 15 : limit;
    Page<DeviceVo> voPage = new Page<>(page, limit);
    return deviceService.selectAllDevice(voPage, deviceType);
  }

  @ApiOperation("查询led最近内容")
  @GetMapping("/selectLedHistory")
  public ResponseVo selectLedHistory(String deviceCode) {
    QueryWrapper<LedHistory> queryWrapper = new QueryWrapper<>();
    queryWrapper.orderBy(true, false, "create_time").eq("device_mac", deviceCode);
    return ResponseVo.successWithData(ledHistoryService.list(queryWrapper));
  }

  @ApiOperation("查询告警设备图表")
  @GetMapping("/selectWarnDeviceCharts")
  public ResponseVo selectWarnProblemsCharts(int month) {
    return deviceService.selectWarnProblemsCharts(month);
  }
}
